package porter

import (
	"context"
	"github.com/tidwall/gjson"
	"shop/src/model"
	"shop/src/open/pdd"
	"time"
)

var pddClient = pdd.NewClient()

// 订单 https://open.pinduoduo.com/application/document/api?id=pdd.order.list.get
type pddTradeFetcher struct {
	store *model.Store
}

func (f *pddTradeFetcher) fetch(startTime time.Time, endTime time.Time, pageNo int, parameter any, hasTotal bool) (any, error) {
	result, err := pddClient.Execute(context.Background(), "pdd.order.list.get", pdd.Parameters{
		"start_confirm_at": startTime.UnixMilli(),
		"end_confirm_at":   endTime.UnixMilli(),
		"order_status":     parameter.(int),
		"page":             pageNo,
		"pageSize":         f.pageSize(),
		"use_has_next":     !hasTotal,
	})
	if err != nil {
		return nil, err
	}
	return result.Get("order_list_get_response"), nil
}

func (f *pddTradeFetcher) hasTotal() bool {
	return false
}

func (f *pddTradeFetcher) count(resp any) int {
	result := resp.(gjson.Result)
	return int(result.Get("total_count").Int())
}

func (f *pddTradeFetcher) buildDocument(resp any) []model.Document {
	result := resp.(gjson.Result)
	list := result.Get("order_list").Array()
	documents := make([]model.Document, len(list))
	for _, it := range list {
		var document model.Document
		document.Sn = it.Get("order_sn").String()
		document.Doc = it.String()
		documents = append(documents, document)
	}
	return documents
}

func (f *pddTradeFetcher) pageSize() int {
	return 100
}

func (f *pddTradeFetcher) startPage() int {
	return 1
}

func (f *pddTradeFetcher) parameters() []any {
	return []any{5}
}

// 退单 https://open.pinduoduo.com/application/document/api?id=pdd.refund.list.increment.get
type pddRefundFetcher struct {
	store *model.Store
}

func (f *pddRefundFetcher) fetch(startTime time.Time, endTime time.Time, pageNo int, parameter any, _ bool) (any, error) {
	result, err := pddClient.Execute(context.Background(), "pdd.order.list.get", pdd.Parameters{
		"start_updated_at": startTime.UnixMilli(),
		"end_updated_at":   endTime.UnixMilli(),
		"after_sales_type": parameter.(int),
		"page":             pageNo,
		"pageSize":         f.pageSize(),
	})
	if err != nil {
		return nil, err
	}
	return result.Get("order_list_get_response"), nil
}

func (f *pddRefundFetcher) hasTotal() bool {
	return false
}

func (f *pddRefundFetcher) count(resp any) int {
	result := resp.(gjson.Result)
	return int(result.Get("total_count").Int())
}

func (f *pddRefundFetcher) buildDocument(resp any) []model.Document {
	result := resp.(gjson.Result)
	list := result.Get("refund_list").Array()
	documents := make([]model.Document, len(list))
	for _, it := range list {
		var document model.Document
		document.Sn = it.Get("order_sn").String()
		document.Rsn = it.Get("id").String()
		document.Doc = it.String()
		documents = append(documents, document)
	}
	return documents
}

func (f *pddRefundFetcher) pageSize() int {
	return 100
}

func (f *pddRefundFetcher) startPage() int {
	return 1
}

func (f *pddRefundFetcher) parameters() []any {
	return []any{1}
}
